package com.wcgksoft.controller.passWord;

import com.wcgksoft.common.util.FileHandleUtil;
import com.wcgksoft.common.util.FileUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MultipartFileToFile;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.system.UserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.passWord
 * @Description TODO
 * @date 2020/5/24 12:15
 * @ClassName PassWordController
 */
@Controller
@RequestMapping("system/passWord")
public class PassWordController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "updatePassWord")
    public Layui updatePassWord(@RequestParam(value = "passWord",required = false)String passWord){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        sysUser.setPassWord(passWord);
        userService.saveMerge(sysUser);
        return Layui.data(0, "success", 1, null);
    }

    @ResponseBody
    @RequestMapping(value = "updateInfo")
    public Layui updateInfo(
            HttpServletRequest request,
            @RequestParam(value = "data",required = false)String data,
            @RequestParam(value = "file",required = false)MultipartFile file
                            ) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        String name = (String) param.get("name");
        if (StringUtils.isNotEmpty(name)){
            sysUser.setName(name);
        }
        String mobile = (String) param.get("mobile");
        if (StringUtils.isNotEmpty(mobile)){
            sysUser.setMobile(mobile);
        }
        String idCard = (String) param.get("idCard");
        if (StringUtils.isNotEmpty(idCard)){
            sysUser.setIdCard(idCard);
        }
        String address = (String) param.get("address");
        if (StringUtils.isNotEmpty(address)){
            sysUser.setAddress(address);
        }
        String email = (String) param.get("email");
        if (StringUtils.isNotEmpty(email)){
            sysUser.setEmail(email);
        }
        String bankName = (String) param.get("bankName");
        if (StringUtils.isNotEmpty(bankName)){
            sysUser.setBankName(bankName);
        }
        String bankNum = (String) param.get("bankNum");
        if (StringUtils.isNotEmpty(bankNum)){
            sysUser.setBankNum(bankNum);
        }
        if (!file.isEmpty()) {
            String filename = FileHandleUtil.Upset(file);
            sysUser.setImage("upload/picture/" + FileHandleUtil.upload(file.getInputStream(), "upload/picture/", filename, request));
        }
        userService.saveMerge(sysUser);
        return Layui.data(0, "success", 1, null);
    }

    
}
